package cn.afactory.tygz.sns.qq;


import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;

import com.tencent.connect.auth.QQAuth;
import com.tencent.connect.share.QQShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.UiError;

import cn.afactory.tygz.sns.Constants;
import cn.afactory.tygz.sns.SnsErrorCode;
import cn.afactory.tygz.sns.lsn.OnSnsResponseListener;

public class QQController {
	private static QQController Instance;
	private Context mContext;
	private Activity mActivity;
	private QQShare mQQShare = null;
	public static QQAuth mQQAuth;

	public static QQController getInstance(Context context, Activity activity) {
		if (Instance == null) {
			Instance = new QQController(context, activity);
		}
		return Instance;
	}

	public QQController(Context context, Activity activity) {
		mContext = context;
		mActivity = activity;
	}

	public void init() {
		mQQAuth = QQAuth.createInstance(Constants.QQ_APP_ID, mActivity);
		mQQShare = new QQShare(mActivity, mQQAuth.getQQToken());
	}

	private OnSnsResponseListener mSnsResponseListener;
	public void sendMessage(
			String title,
			String targetUrl,
			String summary,
			String imageUrl, 
			String imageLocal,
			String appName,
			OnSnsResponseListener lsn) {
		final Bundle params = new Bundle();
		
		mSnsResponseListener = lsn;
		int shareType = QQShare.SHARE_TO_QQ_TYPE_DEFAULT;
		if(!TextUtils.isEmpty(imageUrl) || !TextUtils.isEmpty(imageLocal)){
			shareType = QQShare.SHARE_TO_QQ_TYPE_IMAGE;
		}
		
		if(!TextUtils.isEmpty(title)){
			params.putString(QQShare.SHARE_TO_QQ_TITLE, title);
		}
		if(!TextUtils.isEmpty(targetUrl)){
			params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, targetUrl);
		}else{
			params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,"http://news.qq.com");
		}
		if(!TextUtils.isEmpty(summary)){
			params.putString(QQShare.SHARE_TO_QQ_SUMMARY, summary);
		}
		
		if(!TextUtils.isEmpty(imageLocal)){
			params.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, imageLocal);
		}
		
		if(!TextUtils.isEmpty(imageUrl)){
			params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, imageUrl);
		}

		if(!TextUtils.isEmpty(appName)){
			params.putString(QQShare.SHARE_TO_QQ_APP_NAME, appName);
		}else{
			params.putString(QQShare.SHARE_TO_QQ_APP_NAME, mContext.getResources().getString(mContext.getApplicationInfo().labelRes));
		}
		params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, shareType);
		doShareToQQ(params);
	}

	private void doShareToQQ(final Bundle params) {
		final Activity activity = mActivity;
		new Thread(new Runnable() {

			@Override
			public void run() {
				mQQShare.shareToQQ(activity, params, new IUiListener() {

					@Override
					public void onCancel() {
						if(mSnsResponseListener != null){
							mSnsResponseListener.response(SnsErrorCode.CANCEL, null);
						}
					}

					@Override
					public void onComplete(Object response) {
						if(mSnsResponseListener != null){
							mSnsResponseListener.response(SnsErrorCode.SUCCESS, null);
						}
					}

					@Override
					public void onError(UiError e) {
						if(mSnsResponseListener != null){
							mSnsResponseListener.response(SnsErrorCode.FAILED, e.errorMessage);
						}
					}

				});
			}
		}).start();
	}
	
	public static boolean isSupported(Context context){
		return true;
	}
}
